/**
 *  Stream Recorder: Records RealPlayer(tm) streams as MP3s
 *  Copyright (C) 2007  David Griffiths
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package dg.radio2ipod.model;

import dg.inx.AbstractModel;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.prefs.Preferences;

/**
 * Bean interface for preferences. This class is a singleton accessed
 * by the getInstance() method.
 * @author davidg
 */
public final class Settings extends AbstractModel {
    private static Preferences prefs = Preferences.userNodeForPackage(
            Settings.class);
    static final String IMPORT_TO_ITUNES = "importToITunes";
    static final String STEREO = "stereo";
    static final String WINDOW_TOP = "windowTop";
    static final String WINDOW_LEFT = "windowLeft";
    static final String WINDOW_WIDTH = "windowWidth";
    static final String WINDOW_HEIGHT = "windowHeight";
    static final int DEFAULT_HEIGHT = 500;
    static final int DEFAULT_WIDTH = 600;

    private static Settings settings = new Settings();

    private static Dimension screenSize = Toolkit.getDefaultToolkit(
            ).getScreenSize();

    /**
     * Prevent external instantiation of this class.
     */
    private Settings() {
    }

    /**
     * Get an instance of the settings bean.
     * @return instance of the settings bean
     */
    public static Settings getInstance() {
        return settings;
    }

    /**
     * Whether or not the MP3 should be imported into iTunes after
     * recording.
     * @return true if should import into iTunes, false otherwise.
     */
    public boolean isImportToITunes() {
        return prefs.getBoolean(IMPORT_TO_ITUNES, true);
    }

    /**
     * Specify whether tracks should be imported into iTunes after
     * recording.
     * @param value true if they should, false otherwise
     */
    public void setImportToITunes(final boolean value) {
        boolean oldValue = isImportToITunes();
        prefs.putBoolean(IMPORT_TO_ITUNES, value);
        firePropertyChange("importToITunes", new Boolean(oldValue),
                new Boolean(value));
    }

    /**
     * Whether recordings be in stereo by default.
     * @return true if stereo by default, false otherwise.
     */
    public boolean isStereo() {
        return prefs.getBoolean(STEREO, false);
    }

    /**
     * Specify whether recordings should be in stereo by default.
     * @param value true if default to stereo, false otherwise.
     */
    public void setStereo(final boolean value) {
        boolean oldValue = isStereo();
        prefs.putBoolean(STEREO, value);
        firePropertyChange("stereo", new Boolean(oldValue),
                new Boolean(value));
    }

    /**
     * y-coordinate of the top-left corner of the main window.
     * @return y-coordinate of the top-left corner of the main window.
     */
    public int getWindowTop() {
        int dflt = (screenSize.height - getWindowHeight()) / 2;
        return prefs.getInt(WINDOW_TOP, dflt);
    }

    /**
     * Set y-coordinate of the top-left corner of the main window.
     * @param value y-coordinate of the top-left corner of the main window.
     */
    public void setWindowTop(final int value) {
        int oldValue = getWindowTop();
        prefs.putInt(WINDOW_TOP, value);
        firePropertyChange("windowTop", oldValue, value);
    }

    /**
     * x-coordinate of the top-left corner of the main window.
     * @return x-coordinate of the top-left corner of the main window.
     */
    public int getWindowLeft() {
        int dflt = (screenSize.width - getWindowWidth()) / 3;
        return prefs.getInt(WINDOW_LEFT, dflt);
    }

    /**
     * Set the x-coordinate of the top-left corner of the main window.
     * @param value x-coordinate of the top-left corner of the main window.
     */
    public void setWindowLeft(final int value) {
        int oldValue = getWindowLeft();
        prefs.putInt(WINDOW_LEFT, value);
        firePropertyChange("windowLeft", oldValue, value);
    }

    /**
     * Width of the main window.
     * @return width of the main window.
     */
    public int getWindowWidth() {
        int dflt = DEFAULT_WIDTH;
        return prefs.getInt(WINDOW_WIDTH, dflt);
    }

    /**
     * Set the width of the main window.
     * @param value width of the main window.
     */
    public void setWindowWidth(final int value) {
        int oldValue = getWindowWidth();
        prefs.putInt(WINDOW_WIDTH, value);
        firePropertyChange("windowWidth", oldValue, value);
    }

    /**
     * Height of the main window.
     * @return height of the main window.
     */
    public int getWindowHeight() {
        int dflt = DEFAULT_HEIGHT;
        return prefs.getInt(WINDOW_HEIGHT, dflt);
    }

    /**
     * Set the height of the main window.
     * @param value height of the main window.
     */
    public void setWindowHeight(final int value) {
        int oldValue = getWindowHeight();
        prefs.putInt(WINDOW_HEIGHT, value);
        firePropertyChange("windowHeight", oldValue, value);
    }
}
